<?php
class system_controller_action_helper {
	protected static $_helpers = array();
	protected $_actionController;
	
	public function __construct(system_controller_action $actionController) {
		$this->_actionController = $actionController;
	}
	
	public function __call($method, $args) {
		$helper = $this->getHelper($method);
		if(!method_exists($helper, 'direct')) {
			throw new system_exception('The helper can not method direct');
		}
		return call_user_func(array($helper, 'direct'), $args);
	}
	public function __get($name) {
		return $this->getHelper($name);
	}
	
	public function addHelper(system_controller_action_helper_abstract $helper, $name = null) {
		if(null === $name) {
			$name = $helper->getName();
		}
		self::$_helpers[$name] = $helper;
		return $this;
	}
	
	public function getHelper($name) {
		if(!isset(self::$_helpers[$name])) {
			try {
				$class = 'system_controller_action_helper_' . $name;
				$helper = new $class();
				$helper->setActionController($this->_actionController);
				$helper->init();
				self::$_helpers[$name] = $helper;
			} catch (Exception $e) {
				throw $e;
			}
		}
		return self::$_helpers[$name];
	}

}